package com.example.studentsystem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.studentsystem.Bean.Student;
import com.example.studentsystem.db.DBHelper;


public class RegisterActivity extends AppCompatActivity {

    private EditText mEdit1;
    private EditText mEdit2;
    private EditText mEdit3;
    private EditText mEdit4;
    private EditText mEdit5;
    private Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mEdit1=findViewById(R.id.register_edit1);
        mEdit2=findViewById(R.id.register_edit2);
        mEdit3=findViewById(R.id.register_edit3);
        mEdit4=findViewById(R.id.register_edit4);
        mEdit5=findViewById(R.id.register_edit5);
        mButton=findViewById(R.id.register_button);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=mEdit1.getText().toString();
                String className= mEdit2.getText().toString();
                String code = mEdit3.getText().toString();
                String username = mEdit4.getText().toString();
                String password= mEdit5.getText().toString();
                if (v.getId() == R.id.register_button){
                    if (allInputNotEmpty(name, className,code, username, password)) {
                        Student student = new Student();
                        student.setClass_name(className);
                        student.setStudent_code(code);
                        student.setStudent_name(name);
                        student.setUsername(username);
                        student.setPassword(password);
                        int id = (int) DBHelper.getInstance(RegisterActivity.this).insertStudent2(student);
                        if (id >= 0) {
                            Toast.makeText(RegisterActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                            mEdit1.setText("");
                            mEdit2.setText("");
                            mEdit3.setText("");
                            mEdit4.setText("");
                            mEdit5.setText("");
                        }else {
                            Toast.makeText(RegisterActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
            private boolean allInputNotEmpty(String... inputStrings) {
                for (int i = 0; i < inputStrings.length; i++) {
                    if (TextUtils.isEmpty(inputStrings[i])) {
                        return false;
                    }
                }
                return true;
            }
        });

    }
}